package easamples;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Diagram;
import org.sparx.DiagramObject;
import org.sparx.Element;
import org.sparx.Repository;


public class ManageDiagramsExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Add and Manage Diagrams";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "An example of how to create a diagram and add an Element to it.<br/><br/><b>Related APIs</b><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?diagram2.htm'>Diagram API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?diagramobjects.htm'>Diagram Object API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// Create a new package in the first model node (will throw an exception if there
				// are no model nodes)
				org.sparx.Package modelNode = rep.GetModels().GetAt( (short)0 );
				org.sparx.Package testPackage = modelNode.GetPackages().AddNew( "TestPackage", "Class" );
				testPackage.Update();
				outputList.addElement( "Added package '" + testPackage.GetName() + "' to model" );
				
				// Create a diagram in the test package
				Diagram testDiagram = testPackage.GetDiagrams().AddNew( "My Logical Diagram", "Logical" );
				testDiagram.SetNotes( "Test diagram created by the automation interface example app" );
				testDiagram.Update();
				outputList.addElement( "Added diagram '" + testDiagram.GetName() 
						+ "' to package '" + testPackage.GetName() + "'");
				
				// Create an element (which will be added to the diagram later on)
				Element testElement = testPackage.GetElements().AddNew( "TestClass", "Class" );
				testElement.Update();
				outputList.addElement( "Created element '" + testElement.GetName() 
						+ "' in package '" + testPackage.GetName() + "'");
				
				//	Add the element to the diagram
				Collection<DiagramObject> diagramObjects = testDiagram.GetDiagramObjects();
				String styleString = "l=200;r=400;t=200;b=600;";
				DiagramObject testDO = diagramObjects.AddNew( styleString, "" );
				testDO.SetElementID( testElement.GetElementID() );
				testDO.Update();
				
				outputList.addElement( "Added element '" + testElement.GetName() 
						+ "' to diagram '" + testDiagram.GetName() + "'");
				
				outputList.addElement( "Done!" );
				
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
